<?php

namespace App\Http\Controllers;

use App\Models\Classes;
use App\Models\Student;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class StudentController extends Controller
{
    /**
     * 学生页面
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
     */
    public function index()
    {
        $classData = Classes::query()->get();
        return view('student', ['classData' => $classData]);
    }

    /**
     * 获取学生列表数据
     * @return array
     */
    public function getList()
    {
        $query = Student::query();
        $count = $query->count();
        $data = $query->with('oneClass')->get();
        return [
            'code' => 0,
            'msg' => '获取成功',
            'count' => $count,
            'data' => $data
        ];
    }

    /**
     * 添加学生信息
     * @param Request $request
     * @return array
     */
    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required|string|max:50',
            'class_id' => 'required|integer',
        ], [
            'name.required' => '学生名称不能为空',
            'name.string' => '学生名称必须为字符串',
            'name.max' => '学生名称不能超过50个字',
            'class_id.required' => '班级不能为空',
            'class_id.integer' => '班级必须为整型',
        ]);

        $errors = $validator->errors();
        if ($errors->first()) {
            return [
                'code' => 1,
                'msg' => $errors->first(),
                'data' => []
            ];
        }

        $data = $request->only([
            'name',
            'class_id',
        ]);

        $result = Student::query()->create($data);
        if (!$result) {
            return [
                'code' => 1,
                'msg' => '添加失败',
                'data' => []
            ];
        }

        return [
            'code' => 0,
            'msg' => '添加成功',
            'data' => []
        ];
    }

    /**
     * 修改学生信息
     * @param Request $request
     * @return array
     */
    public function update(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
            'name' => 'required|string|max:50',
            'class_id' => 'required|integer',
        ], [
            'id.required' => '缺少学生ID',
            'id.integer' => '缺少学生ID',
            'name.required' => '学生名称不能为空',
            'name.string' => '学生名称必须为字符串',
            'name.max' => '学生名称不能超过50个字',
            'class_id.required' => '班级不能为空',
            'class_id.integer' => '班级必须为整型',
        ]);

        $errors = $validator->errors();
        if ($errors->first()) {
            return [
                'code' => 1,
                'msg' => $errors->first(),
                'data' => []
            ];
        }

        $id = $request->get('id');

        $data = $request->only([
            'name',
            'class_id',
        ]);
        $result = Student::query()->where(['id' => $id])->update($data);

        if ($result === false) {
            return [
                'code' => 1,
                'msg' => '修改失败',
                'data' => []
            ];
        }

        return [
            'code' => 0,
            'msg' => '修改成功',
            'data' => []
        ];
    }

    /**
     * 删除学生信息
     * @param Request $request
     * @return array
     */
    public function destroy(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ], [
            'id.required' => '缺少学生ID',
            'id.integer' => '缺少学生ID',
        ]);

        $errors = $validator->errors();
        if ($errors->first()) {
            return [
                'code' => 1,
                'msg' => $errors->first(),
                'data' => []
            ];
        }

        $id = $request->get('id');
        $result = Student::query()->where(['id' => $id])->delete();
        if (!$result) {
            return [
                'code' => 1,
                'msg' => '删除失败',
                'data' => []
            ];
        }

        return [
            'code' => 0,
            'msg' => '删除成功',
            'data' => []
        ];
    }
}
